import * as api from '@/api/index';
import { Button } from 'antd';
import { history } from 'umi';
import { Loading, connect, ConnectProps } from 'umi';
import { useEffect, useState } from 'react';

const LoginCom = (props: any) => {
  let [list, setList] = useState([]);
  // console.log(props.children, 'props.children b login');
  console.log(props);
  useEffect(() => {
    // console.log(32322);
    // props.dispatch({ type: 'pro/getList' });
    setList(props.q.pro.listarr);
  }, [Math.random()]);

  let loginApi = async () => {
    try {
      let res: any = await api.Login('admin', 'admin');
      
      setList(props.q.pro.listarr);
      console.log(res.data.state);

      if (res.data.state) {
        localStorage.setItem('token', res.data.token);
        history.push('/');
      }
    } catch (error) {}
  };
  return (
    <>
      <fieldset>
        <legend>login</legend>
        <span>login</span>
        <Button type="dashed" onClick={loginApi}>
          登录
        </Button>
      </fieldset>
      <ul>
        {/* <div>{list}</div> */}
        {list.map((item: any) => {
          return <li key={item._id}>{item.username}</li>;
        })}
      </ul>
    </>
  );
};

export default connect((q) => ({ q }))(LoginCom);
